﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using chessLogic;

namespace chessView
{
    public partial class ChessView : Form
    {
        private Drawer drawer;
        private Chess chess;
        private Fen fen;
        private bool gameSelected;
        private ChessGUI chessGUI;

        public ChessView()
        {
            InitializeComponent();
            drawer = new Drawer();
            chess = new Chess();
            fen = new Fen();
            chessGUI = new ChessGUI();
            gameSelected = false;
        }

        private void Chess_Paint(object sender, PaintEventArgs e)
        {
            drawer.DrawBoard(e.Graphics);
            if (chess.currentGame.MoveNumber != 0)
            {
                drawer.DrawMoveSquare(e.Graphics, chess.board.NewPosition);
                drawer.DrawMoveSquare(e.Graphics, chess.board.OldPosition);
            }
            if (gameSelected)
                drawer.DrawFiguresOnBoard(e.Graphics, chess.board);
        }

        #region Buttons
        private void forward_Click(object sender, EventArgs e)
        {
            PlayGame();
        }

        private void play_Click(object sender, EventArgs e)
        {
            if (moveTimer.Enabled == true)
                play.Text = "Play";
            else
                play.Text = "Stop";
            moveTimer.Enabled = !moveTimer.Enabled;
            if (chess.currentGame.MoveNumber - 1 > 0)
            {
                gameMoves.Focus();
                gameMoves.Items[chess.currentGame.MoveNumber - 1].Focused = true;
                gameMoves.Items[chess.currentGame.MoveNumber - 1].Selected = true;
            }
        }

        private void _back_Click(object sender, EventArgs e)
        {
            int indexBackMove = chess.currentGame.MoveNumber - 1;
            chess.InitialChess();
            while (chess.currentGame.MoveNumber < indexBackMove)
                PlayGame();
            ChessPanel.Invalidate();
        }

        private void start_Click(object sender, EventArgs e)
        {
            chess.InitialChess();
            ChessPanel.Invalidate();
        }

        private void end_Click(object sender, EventArgs e)
        {
            while (!chess.currentGame.IsFinished())
                PlayGame();
        }

        private void searchToFen_Click(object sender, EventArgs e)
        {
            if (gameSelected)
            {
                fen.CreateFen(chess.board, chess.currentGame.getWhoMove());
                SelectForm selectForm = new SelectForm(fen.FenString);
                selectForm.ShowDialog(this);
                if (selectForm.selectedGame != null)
                {
                    chess.currentGame = selectForm.selectedGame;
                    setGameTag();
                    setControl();
                    chess.InitialChess();
                    gameSelected = true;
                    setGameMoves(chess.currentGame.Moves);
                    ChessPanel.Invalidate();
                }
            }
        }

        private void selectGame_Click(object sender, EventArgs e)
        {
            SelectForm selectForm = new SelectForm();
            selectForm.ShowDialog(this);
            if (selectForm.selectedGame != null)
            {
                chess.currentGame = selectForm.selectedGame;
                setGameTag();
                setControl();
                chess.InitialChess();
                gameSelected = true;
                setGameMoves(chess.currentGame.Moves);
                ChessPanel.Invalidate();
            }
        }
        private void Statistics_Click(object sender, EventArgs e)
        {
            if (gameSelected)
            {
                fen.CreateFen(chess.board, chess.currentGame.getWhoMove());
                StatisticsForm statForm = new StatisticsForm(fen.FenString);
                statForm.ShowDialog(this);
            }
        }
        #endregion
        private void PlayGame()
        {
            if (!chess.currentGame.IsFinished())
            {
                chess.MakeNextMove();
                ChessPanel.Invalidate();
                gameMoves.Focus();
                gameMoves.Items[chess.currentGame.MoveNumber - 1].Focused = true;
                gameMoves.Items[chess.currentGame.MoveNumber - 1].Selected = true;
            }
            else
            {
                moveTimer.Enabled = false;
                this.play.Text = "Play";
                DialogResult result = MessageBox.Show(chess.currentGame.getWinner());
            }
        }

        private void setGameTag()
        {
            EventVal.Text = chess.currentGame.Event;
            RoundVal.Text = chess.currentGame.Round;
            WhiteVal.Text = chess.currentGame.White;
            BlackVal.Text = chess.currentGame.Black;
            DateVal.Text = chess.currentGame.Date;
            SiteVal.Text = chess.currentGame.Site;
            ResultVal.Text = chess.currentGame.Result;
            ECOVal.Text = chess.currentGame.ECO;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PlayGame();
        }
        private void setGameMoves(List<string> moves)
        {
            gameMoves.Clear();
            bool whiteMove = true;
            int moveIndex = 1;
            foreach (string move in moves)
            {
                if (whiteMove == true)
                    gameMoves.Items.Add((moveIndex++) + ". " + move);
                else
                    gameMoves.Items.Add(move);
                whiteMove = !whiteMove;
            }
        }

        private void gameMoves_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (gameMoves.FocusedItem != null)
            {
                int necessaryMove = gameMoves.Items.IndexOf(gameMoves.FocusedItem) + 1;
                chess.currentGame.MoveNumber = 0;
                chess.InitialChess();
                while (chess.currentGame.MoveNumber < necessaryMove)
                    chess.MakeNextMove();
                ChessPanel.Invalidate();
            }
        }
        private void setControl()
        {
            this.play.Enabled = true;
            this.forward.Enabled = true;
            this._back.Enabled = true;
            this.start.Enabled = true;
            this.end.Enabled = true;
        }

        private void ChessPanel_MouseClick(object sender, MouseEventArgs e)
        {
            if (gameSelected && !chess.board.getFigure(chessGUI.getPositionClickOnBoard(e)).Equals(new None()))
            {
                BoardForm boardForm = new BoardForm(chess.board.getFigure(chessGUI.getPositionClickOnBoard(e)));
                boardForm.ShowDialog(this);
            }
        }
    }
}

